define(['jquery', 'static', 'underscore', 'layui', 'echarts', 'easyui', 'pagination', 'examHandle', 'repairMsg', 'text!../template/home.html', 'css!../css/home', 'common', 'easyui'],
	function ($, static, _, layui, echarts, easyui, pagination, examHandle, repairMsg, tplHome) {

		var _home = {};
		let currentPage = 1;
		let currentPage1 = 1;
		let currentType = '';
		var tpls = {
			'tplHome': _.template(tplHome)
		};
		_home.show = function () {
			currentPage = 1;
			currentPage1 = 1;
			$('#contant').html(tpls.tplHome());
			Ajax("/xjrest/index/sum_count", function (data) {
				$('#routeSum').html(data.ret.pointNum);
				$('#inspectSum').html(data.ret.insUserNum);
			});
			Ajax("/xjrest/index/task_count", function (data) {
				if (data.ret.success) {
					setChart(data.ret.taskCount);
					setChart3(data.ret.areaCount);
				}
			});
			Ajax("/xjrest/index/repair_count", function (data) {
				if (data.ret.success) {
					setChart1(data.ret.maintainCount);
					setChart2(data.ret.maintainCount);
				}
			});

			Ajax('/xjrest/audit/earliest_aduit', function (result) {
				let mapData = result.ret.value;
				drawMap(mapData);
			});

			$('.auditType').click(function (event) {
				$('.auditType').removeClass('selected');
				$(this).addClass('selected');
				currentType = $(this).attr('id');
				currentPage = 1;

				if (currentType == 'auditList') {
					$(".allmap").hide();
					$(".examineTable").show();

					setHomeExamine(0, 100);
				} else if (currentType == 'auditMap') {
					$(".examineTable").hide();
					$(".allmap").show();

					Ajax('/xjrest/audit/earliest_aduit', function (result) {
						let mapData = result.ret.value;
						drawMap(mapData);
					});
				}
			});

			$('.auditType').eq(0).click();

			setTable2(0, 5);
		}

		function drawMap(data) {
			let allpoint = [];
			let mapData = [];
			$.each(data, function (index, el) {
				if (el.longitude != '' && el.latitude != '') {
					mapData.push(el)
				}
			});
			let map = new BMap.Map("allmap");
			map.enableScrollWheelZoom(true);
			let infoWindows = [];
			var markers = [];
			$.each(mapData, function (index, el) {
				allpoint.push({
					"lng": el.longitude,
					"lat": el.latitude
				});
				let pt = new BMap.Point(el.longitude, el.latitude);
				let myIcon = new BMap.Icon("./img/map/normal.png", new BMap.Size(30, 30));
				let marker = new BMap.Marker(pt, {
					icon: myIcon,
					title: el.auditTitle
				});
				markers.push(marker);
				markers[index].addEventListener('click', function () {
					examine(el);

					Ajax('/xjrest/audit/earliest_aduit', function (result) {
						let mapData = result.ret.value;
						drawMap(mapData);
					});
				});
				map.addOverlay(marker);
			});
			let view = map.getViewport(eval(allpoint));
			map.centerAndZoom(view.center, view.zoom);
			map.addControl(new BMap.MapTypeControl({
				mapTypes: [BMAP_NORMAL_MAP, BMAP_HYBRID_MAP]
			}));
			map.setMapStyleV2({
				styleJson: [{
					"featureType": "poilabel",
					"elementType": "all",
					"stylers": {
						"visibility": "off"
					}
				}]
			});
		}

		function setChart(data) {
			$("#otNum").html(data.otNum);
			$("#doneNum").html(data.doneNum);
			$("#toDoNum").html(data.toDoNum);
			let total = data.otNum + data.doneNum + data.toDoNum;
			let otNum = data.otNum == 0 ? 0 : data.otNum / total * 100;
			let doneNum = data.doneNum == 0 ? 0 : data.doneNum / total * 100;
			let toDoNum = data.toDoNum == 0 ? 0 : data.toDoNum / total * 100;
			let dataStyle = {
				normal: {
					label: {
						show: false
					},
					labelLine: {
						show: false
					}
				}
			};
			let placeHolderStyle = {
				normal: {
					color: 'rgba(54, 117, 200,0.1)',
					label: {
						show: false
					},
					labelLine: {
						show: false
					}
				},
				emphasis: {
					color: 'rgba(54, 117, 200,0.1)'
				}
			};
			option = {
				color: ['#4BBAFF', '#548CFF', '#FFC525'],
				series: [{
						name: '1',
						type: 'pie',
						radius: [60, 75],
						itemStyle: dataStyle,
						data: [{
								value: doneNum,
								name: '已巡检'
							},
							{
								value: 100 - doneNum,
								name: 'invisible',
								itemStyle: placeHolderStyle
							}
						]
					},
					{
						name: '2',
						type: 'pie',
						radius: [40, 55],
						itemStyle: dataStyle,
						data: [{
								value: toDoNum,
								name: '待巡检'
							},
							{
								value: 100 - toDoNum,
								name: 'invisible',
								itemStyle: placeHolderStyle
							}
						]
					},
					{
						name: '3',
						type: 'pie',
						radius: [20, 35],
						itemStyle: dataStyle,
						data: [{
								value: otNum,
								name: '已超时'
							},
							{
								value: 100 - otNum,
								name: 'invisible',
								itemStyle: placeHolderStyle
							}
						]
					}
				]
			};
			let dom = document.getElementById("ChartBody");
			let myChart = echarts.init(dom);
			myChart.setOption(option, true);
			myChart.resize();
			window.addEventListener("resize", function () {
				myChart.resize();
			});
		}

		function setChart1(data) {
			$("#doneNum1").html(data.doneNum);
			$("#todoNum").html(data.todoNum);
			let total = data.doneNum + data.todoNum;
			let doneNum = data.doneNum == 0 ? 0 : data.doneNum / total * 100;
			let todoNum = data.todoNum == 0 ? 0 : data.todoNum / total * 100;
			let dataStyle = {
				normal: {
					label: {
						show: false
					},
					labelLine: {
						show: false
					}
				}
			};
			let placeHolderStyle = {
				normal: {
					color: 'rgba(54, 117, 200,0.1)',
					label: {
						show: false
					},
					labelLine: {
						show: false
					}
				},
				emphasis: {
					color: 'rgba(54, 117, 200,0.1)'
				}
			};
			option = {
				color: ['#548CFF', '#FFC525'],
				series: [{
						name: '2',
						type: 'pie',
						radius: [40, 55],
						itemStyle: dataStyle,
						data: [{
								value: doneNum,
								name: '已维修'
							},
							{
								value: 100 - doneNum,
								name: 'invisible',
								itemStyle: placeHolderStyle
							}
						]
					},
					{
						name: '3',
						type: 'pie',
						radius: [20, 35],
						itemStyle: dataStyle,
						data: [{
								value: todoNum,
								name: '待维修'
							},
							{
								value: 100 - todoNum,
								name: 'invisible',
								itemStyle: placeHolderStyle
							}
						]
					}
				]
			};
			let dom = document.getElementById("ChartBody1");
			let myChart = echarts.init(dom);
			myChart.setOption(option, true);

			window.addEventListener("resize", function () {
				myChart.resize();
			});
		}

		function setChart2(data) {
			$("#impNum").html(data.impNum);
			$("#commonNum").html(data.commonNum);
			let total = data.commonNum + data.impNum;
			let impNum = data.impNum == 0 ? 0 : data.impNum / total * 100;
			let commonNum = data.commonNum == 0 ? 0 : data.commonNum / total * 100;
			let dataStyle = {
				normal: {
					label: {
						show: false
					},
					labelLine: {
						show: false
					}
				}
			};
			let placeHolderStyle = {
				normal: {
					color: 'rgba(54, 117, 200,0.1)',
					label: {
						show: false
					},
					labelLine: {
						show: false
					}
				},
				emphasis: {
					color: 'rgba(54, 117, 200,0.1)'
				}
			};
			option = {
				color: ['#4BBAFF', '#31E4AA'],
				series: [{
						name: '2',
						type: 'pie',
						radius: [40, 55],
						itemStyle: dataStyle,
						data: [{
								value: impNum,
								name: '紧急维修'
							},
							{
								value: 100 - impNum,
								name: 'invisible',
								itemStyle: placeHolderStyle
							}
						]
					},
					{
						name: '3',
						type: 'pie',
						radius: [20, 35],
						itemStyle: dataStyle,
						data: [{
								value: commonNum,
								name: '一般维修'
							},
							{
								value: 100 - commonNum,
								name: 'invisible',
								itemStyle: placeHolderStyle
							}
						]
					}
				]
			};
			let dom = document.getElementById("ChartBody2");
			let myChart = echarts.init(dom);
			myChart.setOption(option, true);

			window.addEventListener("resize", function () {
				myChart.resize();
			});
		}

		function setChart3(data) {
			var myChart2 = echarts.init(document.getElementById('allChartBody'));
			var chartData = [];
			data.reverse();
			$.each(data, function (index, el) {
				for (key in el) {
					chartData.push({
						'name': key,
						'value': el[key]
					});
				}
			});
			var valueChart = [];
			var valueX = [];
			var backgroundData = [];

			$.each(chartData, function (n, val) {
				valueChart.push(val.value);
				valueX.push(val.name);
			})

			$.each(chartData, function (n, val) {
				backgroundData.push({
					'value': Math.max.apply(null, valueChart) + 2,
					'reallyData': val.value
				});
			})
			var option2 = {
				grid: {
					x: "25%",
					y: "2%",
					y2: "10%",
					x2: "10%",
					borderWidth: 0
				},
				calculable: true,
				tooltip: {
					trigger: 'axis',
					formatter: '{b1}:{c0}',
				},
				xAxis: [{
					type: 'value',
					max: Math.max.apply(null, valueChart) + 2,
				}],
				yAxis: [{
						position: 'left',
						type: 'category',
						axisTick: {
							show: false
						},
						splitLine: {
							show: false,
						},
						splitArea: {
							show: false,
						},
						axisLine: {
							show: false
						},
						axisLabel: {
							textStyle: {
								color: "#353535",
								fontSize: "14",
							},
						},
						data: valueX,
					},
					{
						type: 'category',
						axisLine: {
							show: false
						},
						axisTick: {
							show: false
						},
						axisLabel: {
							show: false
						},
						splitArea: {
							show: false
						},
						splitLine: {
							show: false
						},
						data: valueX,
					}
				],
				series: [{
						name: '',
						type: 'bar',
						yAxisIndex: 1,
						barWidth: 10,
						itemStyle: {
							normal: {
								color: 'rgba(54, 117, 200,0.2)',
								borderColor: 'rgba(1,252,146,0.2)',
								borderWidth: 1,
								barBorderRadius: 5
							}
						},
						data: backgroundData,
					},
					{
						z: 3,
						name: '各区县分布',
						type: 'bar',
						barWidth: 10,
						stack: 01,
						data: valueChart,
						itemStyle: {
							normal: {
								label: {
									show: false,
									position: 'right',
								},
								color: {
									type: 'linear',
									x: 1,
									y: 1,
									x2: 0,
									y2: 1,
									colorStops: [{
										offset: 0,
										color: '#2dd3fd'
									}, {
										offset: 1,
										color: '#7a80ff'
									}],
									globalCoord: false
								},
								barBorderRadius: 5,
							}
						}
					},
				]
			};

			myChart2.setOption(option2, true);
			$(window).resize(function () {
				myChart2.resize();
			})
		}

		window.setHomeExamine = function (from, count) {
			$('#gridRepairTable').datagrid({
				url: '/xjrest/audit/list_aduit?auditStatus=1&from=' + from + '&count=' + count,
				queryParams: '',
				width: '100%',
				method: 'get',
				border: false,
				showHeader: true,
				//singleSelect: true,
				fitColumns: true,
				scrollbarSize: 0,
				loadMsg: "正在查询,请耐心等待。。。。。。。",
				columns: [
					[{
							field: 'auditId',
							width: "5%",
							height: "32px",
							align: 'center',
							checkbox: true
						},
						{
							field: 'submitTime',
							title: '申请时间',
							width: "20%",
							height: "48px",
							align: 'center'
						},
						{
							field: 'userName',
							title: '申请人',
							width: "25%",
							align: 'center'
						},
						{
							field: 'auditTitle',
							title: '申请事项',
							width: "25%",
							align: 'center',
							formatter: function (value, row) {
								return `<span title="${value}">${truncate(value,12)}</span>`;
							}
						},
						{
							field: 'phone',
							title: '联系电话',
							width: "25%",
							align: 'center',
							hidden: static.currentUser.role == 1 ? true : false
						},
						{
							field: 'auditName',
							title: '操作',
							width: "25%",
							align: 'center',
							formatter: function (value, rowData) {
								let data = JSON.stringify(rowData).replace(/\"/g, "'");
								return `<input class="examButton" type="button" value="审核" onclick="examine(${data})"/>`
							},
							hidden: static.currentUser.role != 1 ? true : false
						}

					]
				],
				loadFilter: function (data) {
					let _data = data.ret.value;
					let totals = data.ret.total;
					if (totals == 0) {
						$("#noRecordMessage").show();
					} else {
						$("#noRecordMessage").hide();
					}
					if (0 == totals % count) {
						totalPage = parseInt(totals / count);
						if (totalPage == 0) {
							totalPage = 1;
						}
					} else {
						totalPage = parseInt(totals / count) + 1;
					}
					$('#pagination').paginations({
						currentPage: currentPage,
						totalPage: totalPage,
						callback: function (current) {
							currentPage = current;
							current = (current - 1) * count;
							setHomeExamine(current, count);
						}
					});
					return _data;
				}
			});
		}

		window.examine = function (data) {
			data.type = 'exIng';
			examHandle.show(data, 1, 1);
		}

		function setTable2(from, count) {
			$('#gridRepairTable1').datagrid({
				url: '/xjrest/main/list_main?from=' + from + '&count=' + count + '&status=1',
				queryParams: '',
				width: '100%',
				method: 'get',
				border: false,
				singleSelect: true,
				fitColumns: false,
				scrollbarSize: 0,
				loadMsg: "正在查询,请耐心等待。。。。。。。",
				columns: [
					[{
							field: 'pointName',
							title: '建设地点',
							width: "11%",
							align: 'center',
							formatter: function (value, row) {
								return `<span title="${value}">${truncate(value,9)}</span>`;
							}
						},
						{
							field: 'manufacturer',
							title: '器材厂家',
							width: "11%",
							align: 'center',
							formatter: function (value, row) {
								return `<span title="${value}">${truncate(value,9)}</span>`;
							}
						},
						{
							field: 'buildTime',
							title: '建设时间',
							width: "11%",
							align: 'center'
						},
						{
							field: 'maintainGrade',
							title: '维修等级',
							width: "11%",
							align: 'center',
							formatter: function (value, rowData) {
								return value == 1 ? '一般维修' : '紧急维修'
							}
						},
						{
							field: 'beginTime',
							title: '维修开始时间',
							width: "11%",
							align: 'center',
							formatter: function (value, rowData) {
								return value.substring(5)
							}
						},
						{
							field: 'endTime',
							title: '维修截止时间',
							width: "11%",
							align: 'center',
							formatter: function (value, rowData) {
								return value.substring(5)
							}
						},
						{
							field: 'equipmentType',
							title: '器材类型',
							width: "11%",
							align: 'center',
							formatter: function (value, rowData) {
								return value == 1 ? '体彩' : '自建'
							}
						},
						{
							field: 'dealName',
							title: '维修人',
							width: "12%",
							align: 'center'
						},
						{
							field: 'phone',
							title: '联系电话',
							width: "11%",
							align: 'center'
						}
					]
				],
				loadFilter: function (data) {
					let _data = data.ret.value;
					let totals = data.ret.total;
					if (totals == 0) {
						$("#noRecordMessage").show();
					} else {
						$("#noRecordMessage").hide();
					}
					if (0 == totals % 5) {
						totalPage = parseInt(totals / 5);
						if (totalPage == 0) {
							totalPage = 1;
						}
					} else {
						totalPage = parseInt(totals / 5) + 1;
					}
					$('#pagination1').paginations({
						currentPage: currentPage1,
						totalPage: totalPage,
						callback: function (current) {
							currentPage1 = current;
							current = (current - 1) * 5;
							setTable2(current, 5);
						}
					});
					return _data;
				},
				onDblClickRow: function (rowIndex, rowData) {
					Ajax("/xjrest/main/main_detail?maintainId=" + rowData.maintainId, function (data) {
						repairMsg.show(data.ret);
					});
				}
			});
		}

		window.auditAll = function () {
			var params = {};
			//获取所选的auditId
			var checkedId = $('#gridRepairTable').datagrid('getChecked');

			if (null == checkedId || checkedId.length == 0) {
				layer.msg('请勾选要审核的信息！');
			} else {
				layer.confirm('确定所选一键审核吗？', {
					btn: ['确定', '取消'] //按钮
				}, function () {
					params.auditIds = [];
					$.each(checkedId, function (index, item) {
						params.auditIds.push(item.auditId);
					});

					Ajax("/xjrest/audit/audit_all", auditAllBack, params);
					layer.closeAll();
				}, function () {

				});
			}

		}

		function auditAllBack(data) {
			if (data.ret.success) {
				layer.msg('审核成功');
			} else {
				layer.msg('审核失败');
			}

			Ajax('/xjrest/audit/earliest_aduit', function (result) {
				let mapData = result.ret.value;
				drawMap(mapData);
			});

			currentPage = 1;
			setHomeExamine(0, 100);
		}

		return _home;
	})